package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<String> saveCart(@RequestBody ShoppingCart shoppingCart) {
        Long userId = BaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        ShoppingCart shoppingCart1=new ShoppingCart();
        //判断添加到购物车里的是菜品还是套餐
        if(shoppingCart.getDishId()!=null){
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            shoppingCart1=shoppingCartService.getOne(lambdaQueryWrapper);

        }
        else if(shoppingCart.getSetmealId()!=null){
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            shoppingCart1=shoppingCartService.getOne(lambdaQueryWrapper);
        }
        //判断购物车中的商品是否存在，存在就更新number，否则添加到购物车表
        if (shoppingCart1!=null){
            Integer number = shoppingCart1.getNumber();
            number++;
            shoppingCart1.setNumber(number);
            shoppingCart1.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(shoppingCart1);
        }
        else {
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }
        return R.success("添加购物车成功");
    }
    @GetMapping("/list")
    public R<List<ShoppingCart>> shoppingCartList(){
        Long userId = BaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(userId!=null,ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(lqw);
        return R.success(shoppingCarts);
    }
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        Long userId = BaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(userId!=null,ShoppingCart::getUserId,userId);
        ShoppingCart object=new ShoppingCart();
        if(dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
             object = shoppingCartService.getOne(lqw);
        }
        else {
            lqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            object=shoppingCartService.getOne(lqw);
        }
        Integer number = object.getNumber();
        if(number>1){
            object.setNumber(number-1);
            shoppingCartService.updateById(object);
        }
        else {
            shoppingCartService.removeById(object.getId());
        }
        return R.success("减少数量成功");
    }
    @DeleteMapping("/clean")
    public R<String> cleanCart(){
        Long userId = BaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(userId!=null,ShoppingCart::getUserId,userId);
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
    }
}
